package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.UserCountService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据统计
 *
 * @author DogeGuo
 */
@DubboService
public class UserCountServiceImpl implements UserCountService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 显示互相喜欢、喜欢、粉丝数量
     *
     * @param userId
     * @return
     */
    @Override
    public Map userLikeEachOther(Long userId) {
        //查询我喜欢的用户
        PageBeanVo iLiked = iLiked(userId, 1, 10);

        //获得我喜欢的好友个数
        Long iLikedCounts = iLiked.getCounts();

        //查询喜欢我的用户
        PageBeanVo likeMe = likeMe(userId, 1, 10);

        //获得粉丝的个数
        Long likeMeCounts = likeMe.getCounts();

        //查询互相喜欢的用户
        PageBeanVo likeEachOther = likeEachOther(userId, 1, 10);

        //获得互相喜欢的个数
        Long likeEachOtherCounts = likeEachOther.getCounts();

        //封装
        Map<String, Object> map = new HashMap<>();
        //互相喜欢
        map.put("eachLoveCount", likeEachOtherCounts);
        //我喜欢
        map.put("loveCount", iLikedCounts);
        //喜欢我
        map.put("fanCount", likeMeCounts);

        return map;
    }

    /**
     * 查询我喜欢的用户
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageBeanVo iLiked(Long userId, Integer pageNum, Integer pageSize) {
        //构造条件
        Query query = Query.query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize);

        //查询我喜欢的用户列表
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        return new PageBeanVo(pageNum, pageSize, count, userLikes);
    }

    /**
     * 查询喜欢我的用户
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageBeanVo likeMe(Long userId, Integer pageNum, Integer pageSize) {
        //构造条件
        Query query = Query.query(Criteria.where("likeUserId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);

        //查询喜欢我的用户列表
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        return new PageBeanVo(pageNum, pageSize, count, userLikes);
    }

    /**
     * 查询相互喜欢的用户
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageBeanVo likeEachOther(Long userId, Integer pageNum, Integer pageSize) {
        //构造条件
        Query query = Query.query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize);

        //查询相互喜欢的用户列表
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);

        return new PageBeanVo(pageNum, pageSize, count, friendList);
    }

    /**
     * 查询互相喜欢、喜欢、粉丝详情
     *
     * @param type
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBeanVo likeDetails(Integer type, Long userId, Integer pageNum, Integer pageSize) {
        switch (type) {
            //1.互相喜欢
            case 1: {
                List<Long> list = new ArrayList<>();
                //查询相互喜欢的用户
                PageBeanVo pageBeanVo = likeEachOther(userId, pageNum, pageSize);

                //获得用户列表，获取用户的id
                List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();
                if (CollectionUtil.isNotEmpty(friendList)) {
                    for (Friend friend : friendList) {
                        list.add(friend.getFriendId());
                    }
                }
                pageBeanVo.setItems(list);

                //返回分页结果
                return pageBeanVo;
            }
            //2.我喜欢
            case 2: {
                List<Long> list = new ArrayList<>();
                //查询我喜欢的用户
                PageBeanVo pageBeanVo = iLiked(userId, pageNum, pageSize);

                //获得用户列表，获取用户的id
                List<UserLike> userLikeList = (List<UserLike>) pageBeanVo.getItems();
                if (CollectionUtil.isNotEmpty(userLikeList)) {
                    for (UserLike userLike : userLikeList) {
                        list.add(userLike.getLikeUserId());
                    }
                }
                pageBeanVo.setItems(list);

                //返回分页结果
                return pageBeanVo;
            }
            //3.喜欢我
            case 3: {
                List<Long> list = new ArrayList<>();

                //查询喜欢我的用户
                PageBeanVo pageBeanVo = likeMe(userId, pageNum, pageSize);

                //获得用户列表，获取用户的id
                List<UserLike> userLikeList = (List<UserLike>) pageBeanVo.getItems();
                if (CollectionUtil.isNotEmpty(userLikeList)) {
                    for (UserLike userLike : userLikeList) {
                        list.add(userLike.getUserId());
                    }
                }
                pageBeanVo.setItems(list);

                //返回分页结果
                return pageBeanVo;
            }
            //4.谁看过我
            case 4: {
                //构造条件
                Query query = Query.query(Criteria.where("userId").is(userId))
                        .with(Sort.by(Sort.Order.desc("date")))
                        .skip((pageNum - 1) * pageSize).limit(pageSize);

                //查询来客列表
                List<Visitor> visitors = mongoTemplate.find(query, Visitor.class);
                //查询来客的数量
                long count = mongoTemplate.count(query, Visitor.class);

                //返回分页结果
                return new PageBeanVo(pageNum, pageSize, count, visitors);
            }
        }
        return null;
    }
}
